home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2001 May / may_2001.iso / intercd / root / Multimedia / ^DivX_Article / virtualdub / VirtualDub-source-1_4d / memcheck.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-03-20  |  2.2 KB  |  110 lines

  1. //    VirtualDub - Video processing and capture application
  2. //    Copyright (C) 1998-2001 Avery Lee
  3. //
  4. //    This program is free software; you can redistribute it and/or modify
  5. //    it under the terms of the GNU General Public License as published by
  6. //    the Free Software Foundation; either version 2 of the License, or
  7. //    (at your option) any later version.
  8. //
  9. //    This program is distributed in the hope that it will be useful,
  10. //    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. //    GNU General Public License for more details.
  13. //
  14. //    You should have received a copy of the GNU General Public License
  15. //    along with this program; if not, write to the Free Software
  16. //    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18. #include <stdlib.h>
  19. #include <windows.h>
  20.  
  21. #if 0
  22.  
  23. void *allocmem(size_t siz) {
  24.     void *mem;
  25.     size_t rsiz = (siz + 4095 + 8) & -4096;
  26.  
  27.     mem = VirtualAlloc(NULL, rsiz, MEM_COMMIT, PAGE_READWRITE);
  28.  
  29.     if (!mem)
  30.         return NULL;
  31.  
  32.     mem = (char *)mem + rsiz - ((siz+7)&-8) - 8;
  33.  
  34.     ((long *)mem)[0] = siz;
  35.     ((long *)mem)[1] = 0x12345678;
  36.  
  37.     return (char *)mem + 8;
  38. }
  39.  
  40. void freemem(void *block) {
  41.     if (!block)
  42.         return;
  43.  
  44.     block = (void *)((char *)block - 8);
  45.  
  46.     if (((long *)block)[1] != 0x12345678)
  47.         __asm int 3
  48.  
  49.     VirtualFree((void *)((long)block & -4096), 0, MEM_RELEASE);
  50. }
  51.  
  52. void *reallocmem(void *block, size_t siz) {
  53.     void *nblock = allocmem(siz);
  54.  
  55.     if (!nblock)
  56.         return NULL;
  57.  
  58.     if (!block)
  59.         return nblock;
  60.  
  61.     if (((long *)block)[-1] != 0x12345678)
  62.         __asm int 3
  63.  
  64.     if (siz < ((long *)block)[-2])
  65.         siz = ((long *)block)[-2];
  66.  
  67.     memcpy(nblock, block, siz);
  68.  
  69.     return nblock;
  70. }
  71.  
  72. void *callocmem(size_t s1, size_t s2) {
  73.     void *mem = allocmem(s1 * s2);
  74.  
  75.     if (!mem)
  76.         return NULL;
  77.  
  78.     memset(mem, 0, s1*s2);
  79.  
  80.     return mem;
  81. }
  82.  
  83. void *operator new(size_t siz) {
  84.     return allocmem(siz);
  85. }
  86. void operator delete(void *block) {
  87.     freemem(block);
  88. }
  89.  
  90.  
  91. #else
  92.  
  93. void *allocmem(size_t siz) {
  94.     return malloc(siz);
  95. }
  96.  
  97. void freemem(void *block) {
  98.     free(block);
  99. }
  100.  
  101. void *reallocmem(void *block, size_t siz) {
  102.     return realloc(block, siz);
  103. }
  104.  
  105. void *callocmem(size_t s1, size_t s2) {
  106.     return calloc(s1, s2);
  107. }
  108.  
  109. #endif
  110.